package poi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelUtil {
	/**
	 * 对外提供读取excel 的方法
	 * 
	 * @throws IOException
	 * */

	public static void main(String[] args) throws IOException {
		File a = new File("c:\\2007.xlsx");
		readExcel(a);
	}

	public static void readExcel(File file) throws IOException {
		String fileName = file.getName();
		String extension = fileName.lastIndexOf(".") == -1 ? "" : fileName
				.substring(fileName.lastIndexOf(".") + 1);
		if ("xls".equals(extension)) {
			read2003Excel(file);
		} else if ("xlsx".equals(extension)) {
			read2007Excel(file);
		} else {
			throw new IOException("不支持的文件类型");
		}
	}

	/**
	 * 读取 office 2003 excel
	 * 
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	private static void read2003Excel(File file) throws IOException {
		String line = "";
		HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));
		HSSFSheet sheet = hwb.getSheetAt(0);
		String value = null;
		HSSFRow row = null;
		HSSFCell cell = null;
		
		System.out.println("该excel共有列："+sheet.getRow(0).getPhysicalNumberOfCells()+" 行："+sheet.getLastRowNum());
		
		for (int i = sheet.getFirstRowNum(); i <= sheet
				.getPhysicalNumberOfRows(); i++) {
			row = sheet.getRow(i);
			if (row == null) {
				continue;
			}
			for (int j = row.getFirstCellNum(); j <= row.getLastCellNum()-1; j++) {
				cell = row.getCell(j);
				if (cell == null) {
					continue;
				}
				value = cell.toString();
				line += value;
				if (value == null || "".equals(value)) {
					continue;
				}
			}
			System.out.println("一行的数据为" + line);
			line = "";
		}
	}

	/**
	 * 读取Office 2007 excel及2007以上excel
	 * */
	private static void read2007Excel(File file) throws IOException {
		String line = "";
		// 构造 XSSFWorkbook 对象，strPath 传入文件路径
		XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));
		// 读取第一章表格内容
		XSSFSheet sheet = xwb.getSheetAt(0);
		String value = null;
		XSSFRow row = null;
		XSSFCell cell = null;
		System.out.println("该excel共有列："+sheet.getRow(1).getPhysicalNumberOfCells()+"     行："+(sheet.getLastRowNum()+1));
		for (int i = sheet.getFirstRowNum(); i <= sheet
				.getPhysicalNumberOfRows(); i++) {
			System.out.println("行号：  "+i);
			row = sheet.getRow(i);
			if (row == null) {
				continue;
			}
			for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
				cell = row.getCell(j);
				if (cell != null) {
					value = cell.toString();
				}
				line+=value;
			}
			System.out.println("第" + i + "行的数据为    :       " + line);
			line = "";
		}
	}
}